package com.mxnet.chongjin.supoman.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.mxnet.chongjin.supoman.R;
import com.mxnet.chongjin.supoman.activity.MachineInstructionActivity;
import com.mxnet.chongjin.supoman.bean.HotMachine;
import com.mxnet.chongjin.supoman.config.Api;
import com.mxnet.chongjin.supoman.utils.LoggerUtils;

import java.util.List;

/**
 * author:chongjin
 * email:lcj270018655@163.com
 * date:2018/4/17
 * other:
 */

public class LvAllMachineAdapter extends BaseAdapter implements View.OnClickListener {
    private Context mContext;
    public List<HotMachine> mList;

    public LvAllMachineAdapter(Context context, List<HotMachine> list) {
        super();
        this.mContext = context;
        this.mList = list;
    }

    @Override
    public int getCount() {
        if (mList.size() % 2 == 0) {
            return mList.size() / 2;
        } else {
            return (mList.size() + 1) / 2;
        }
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position * 2;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_lv_all_machine, parent, false);
            viewHolder = new ViewHolder();
            viewHolder.mLinearLayout1 = convertView.findViewById(R.id.ll_item_all_machine_up);
            viewHolder.mLinearLayout1.setOnClickListener(this);
            viewHolder.mLinearLayout2 = convertView.findViewById(R.id.ll_item_all_machine_down);
            viewHolder.mLinearLayout2.setOnClickListener(this);
            viewHolder.mImageView1 = convertView.findViewById(R.id.iv_item_all_machine_up);
            viewHolder.mImageView2 = convertView.findViewById(R.id.iv_item_all_machine_down);
            viewHolder.mTextView1 = convertView.findViewById(R.id.tv_item_all_machine_up);
            viewHolder.mTextView2 = convertView.findViewById(R.id.tv_item_all_machine_down);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        LoggerUtils.i(mContext.toString() + "=====", position + "");
        Glide.with(mContext).load(Api.URL_HEADER + Api.GET_IMAGE + mList.get(position * 2).getRobotImgUrl()).into(viewHolder.mImageView1);
        viewHolder.mTextView1.setText(mList.get(position * 2).getRobotNo());
        if (position * 2 + 1 < mList.size()) {
            Glide.with(mContext).load(Api.URL_HEADER + Api.GET_IMAGE + mList.get(position * 2 + 1).getRobotImgUrl()).into(viewHolder.mImageView2);
            viewHolder.mTextView2.setText(mList.get(position * 2 + 1).getRobotNo());
        } else {
            viewHolder.mLinearLayout2.setVisibility(View.INVISIBLE);
            viewHolder.mLinearLayout2.setClickable(false);
        }
        return convertView;
    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent();
        switch (v.getId()) {
            case R.id.ll_item_all_machine_up:
                intent.setClass(mContext, MachineInstructionActivity.class);
                intent.putExtra("key", ((TextView) ((LinearLayout) v).getChildAt(1)).getText());
                mContext.startActivity(intent);
                break;
            case R.id.ll_item_all_machine_down:
                intent.setClass(mContext, MachineInstructionActivity.class);
                intent.putExtra("key", ((TextView) ((LinearLayout) v).getChildAt(1)).getText());
                mContext.startActivity(intent);
                break;
        }
    }

    public static class ViewHolder {
        LinearLayout mLinearLayout1, mLinearLayout2;
        ImageView mImageView1, mImageView2;
        TextView mTextView1, mTextView2;
    }
}
